package com.zzgc.modules.controller;

import com.zzgc.common.entity.AuthDepart;
import com.zzgc.common.entity.StudentSchool;
import com.zzgc.common.entity.SysDict;
import com.zzgc.common.entity.SysDictItem;
import com.zzgc.common.response.HttpStatus;
import com.zzgc.common.response.Result;
import com.zzgc.common.utils.StringUtils;
import com.zzgc.modules.log.ApiLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 下拉框 API
 * @Author:
 * @CreateTime: 2022-12-01  14:11
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("dropdown")
@ApiLog(value = "下拉框 API", url = "dropdown")
@Api(tags = "下拉列表")
public class DropdownController extends BaseController {

    @GetMapping("year/list")
    @ApiLog(value = "获取年份列表", url = "year/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取年份列表", notes = "获取年份列表")
    public Result getYears(){
        List<String> list = studentTableDao.queryAllTableName();
        TreeSet<String> treeSet = new TreeSet<>();
        for (String s : list) { // 去重
            // 截取年份并排序
            treeSet.add(s.substring(s.lastIndexOf("_") + 1));
        }
        return Result.builder().code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase()).data(treeSet).build();
    }

    @GetMapping("grades/list")
    @ApiLog(value = "获取年级列表", url = "grades/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取年级列表", notes = "获取年级列表")
    public Result getGrades(){
        SysDictItem sysDictItem = new SysDictItem();
        sysDictItem.setSDictId("3"); // 3小学，4初中，5高中
        List<SysDictItem> sysDictItems = sysDictItemService.queryByPage(sysDictItem);
        sysDictItem.setSDictId("4");
        sysDictItems.addAll(sysDictItemService.queryByPage(sysDictItem));
        sysDictItem.setSDictId("5");
        sysDictItems.addAll(sysDictItemService.queryByPage(sysDictItem));
        return Result.builder().code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase()).data(sysDictItems).build();
    }

    @GetMapping("class/list")
    @ApiLog(value = "获取班级列表", method = RequestMethod.GET , url = "class/list")
    @ApiOperation(value = "获取班级列表", notes = "获取班级列表")
    public Result getClasses(String gradeName){
        StudentSchool studentSchool = new StudentSchool();
        studentSchool.setSGradeName(gradeName);
        List<String> list = studentSchoolService.queryClass(studentSchool);
        return Result.builder().code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase()).data(list).build();
    }

    @GetMapping("school/list")
    @ApiLog(value = "获取学校类型列表", url = "school/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取学校类型列表", notes = "获取学校类型列表")
    public Result getDictSchool(){
        SysDictItem sysDictItem = new SysDictItem();
        sysDictItem.setSDictId("2"); // 2 为学校类型
        List<SysDictItem> sysDictItems = sysDictItemService.queryByPage(sysDictItem);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(sysDictItems)
                .build();
    }

    @GetMapping("city/list")
    @ApiLog(value = "获取类型城市列表", url = "city/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取类型城市列表", notes = "获取市列表")
    public Result getDictCity(){
        SysDictItem sysDictItem = new SysDictItem();
        String userRole = super.getUserRole();
        //判断是否是河南省教育局
        if (!userRole.equals("1") && !userRole.equals("2")) {
            String dist =super.getDepartId();
            AuthDepart depart = departService.queryById(Long.parseLong(dist));
            String city = depart.getAIdBak();
            sysDictItem.setSName(city);
        }

        sysDictItem.setSDictId("6"); // 6为市
        List<SysDictItem> sysDictItems = sysDictItemService.queryByPage(sysDictItem);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(sysDictItems)
                .build();
    }

    @GetMapping("dictItem/list")
    @ApiLog(value = "获取地县级列表", url = "dictItem/list",
            params = "dictId:市级ID", method = RequestMethod.GET)
    @ApiOperation(value = "获取地县级列表", notes = "获取地县区列表")
    public Result getDictItem(String dictName){
        SysDictItem sysDictItem = new SysDictItem();
        SysDict sysDict = sysDictService.queryByName(dictName);
        sysDictItem.setSDictId(sysDict.getId() + "");
        List<SysDictItem> sysDictItems = sysDictItemService.queryByPage(sysDictItem);
        // 如果没有传dictId就返回全部区县
        if (StringUtils.isEmpty(dictName) && sysDictItems.size() >= 15) {
            ArrayList<SysDictItem> sysDictItems1 = new ArrayList<>();
            for (SysDictItem dictItem : sysDictItems) {
                if (Integer.parseInt(dictItem.getSDictId()) <= 6) {
                    sysDictItems1.add(dictItem);
                }
            }
            // 剔除不是区县的信息
            sysDictItems.removeAll(sysDictItems1);
        }
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(sysDictItems)
                .build();
    }
}
